package com.example.applets.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.applets.entity.Collect;
import com.example.applets.service.impl.CollectServiceImpl;
import com.example.applets.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * <p>
 * 收藏 前端控制器
 * </p>
 *
 * @author 云烟成雨丶
 * @since 2020-10-14
 */
@Controller
@RequestMapping("/Kshop/collect")
public class CollectController {
    @Autowired
    private CollectServiceImpl collectService;

    @RequestMapping("/collection")
    @ResponseBody
    //收藏商品
    public AjaxResult collection(@RequestParam("prodId") long prodId, @RequestParam("userId") long userId) {
        QueryWrapper<Collect> wrapper = new QueryWrapper<>();
        wrapper.eq("prodid", prodId);
        wrapper.eq("userid", userId);
        if (collectService.getOne(wrapper) != null)
            return AjaxResult.error("你已经添加,切勿重复!");
        Collect collect = new Collect();
        collect.setProdid(prodId);
        collect.setUserid(userId);
        if (!collectService.save(collect))
            return AjaxResult.error("收藏失败!");
        return AjaxResult.success("收藏成功!");
    }
}

